package cn.melon.view.admin.course;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.melon.mod.sc.ScInfo;
import cn.melon.mod.sc.ScRequest;
import cn.melon.view.admin.course.ctr.CoursePanelCtr;
import cn.melon.view.admin.course.ctr.CourseTableMouse;

public class CourseJPanel extends JPanel implements IUpdateCourseTable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private JTextField Id;
    private JTextField name;
    private JButton queryButton;
    private JButton addButton;
    
    private String[] tableColName = {"课程编号","课程名称","课程教师","上课地点","上课时间"};
    private JTable table;
    DefaultTableModel tableModel;
    private ActionListener listener = new CoursePanelCtr(this);
    private CourseTableMouse mouseListener = new CourseTableMouse(this);
    private List<ScInfo> infoList;      // 保存当前课程信息列表
    private ScRequest request = new ScRequest(this);
    /**
     * Create the panel.
     */
    public CourseJPanel() {
        initialize();
        request.getGetAllScInfoResult();
    }
    
    private void initialize() {
        this.setSize(1200, 1000);
        this.setBackground(Color.black);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel topPanel = new JPanel();
        topPanel.setSize(1200, 50);
        topPanel.setBackground(Color.darkGray);

        JPanel classId = new JPanel();
        JLabel classIdText = new JLabel("课程编号:");
        Id = new JTextField(10);
        classId.add(classIdText);
        classId.add(Id);

        JPanel className = new JPanel();
        JLabel nameText = new JLabel("课程名称:");
        name = new JTextField(10);
        className.add(nameText);
        className.add(name);

        queryButton = new JButton("查询");
        addButton = new JButton("添加课程");

        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
//        topPanel.add(Box.createRigidArea(new Dimension(50, 15)));
        topPanel.add(classId);
        topPanel.add(className);
//        topPanel.add(Box.createRigidArea(new Dimension(50, 15)));
        topPanel.add(queryButton);
        topPanel.add(Box.createRigidArea(new Dimension(400, 15)));
        topPanel.add(addButton);
//        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
        this.add(topPanel);

        JScrollPane tablePanel = new JScrollPane();
        tablePanel.setPreferredSize(new Dimension(360, 650));
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel) {
            private static final long serialVersionUID = 1L;

            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        for(int i=0;i<tableColName.length;i++){
            tableModel.addColumn(tableColName[i]);
        }
        tablePanel.setViewportView(table);
        table.addMouseListener(mouseListener);
        queryButton.addActionListener(listener);
        addButton.addActionListener(listener);
        this.add(tablePanel);
    }
    
    public ScInfo getScInfo() {
        int row = table.getSelectedRows()[0];
        return infoList.get(row);
    }

    @Override
    public void updateTable(List<ScInfo> scList) {
        this.infoList = scList;
        /*
         * 删除全部行
         */
        int rows = tableModel.getRowCount();
        for (int i = 0; i < rows; i++) {
            tableModel.removeRow(0);
        }
        
        /*
         * 添加信息
         */
        for(ScInfo info : infoList){
            String[] course = {info.getClassNumber(), 
                        info.getClassName(), 
                        info.getClassTeacher(),
                        info.getClassroom(),
                        info.getClassTime()
                        };

            tableModel.addRow(course);
        }
    }
}
